package com.ryx.controller;

import com.ryx.aop.annotation.MyLog;
import com.ryx.entity.SysDept;
import com.ryx.service.DeptService;
import com.ryx.utils.DataResult;
import com.ryx.vo.req.DeptAddReqVo;
import com.ryx.vo.req.DeptUpdateReqVo;
import com.ryx.vo.resp.DeptRespNodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @BelongsPackage: com.ryx.controller
 * @Author: 容永轩
 * @CreateTime: 2021-01-01
 * @Description:
 */
@RestController
@RequestMapping("/api")
@Api(tags = "dept Manager Interface")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/depts")
    @ApiOperation(value = "获取机构列表接口")
    @MyLog(title = "组织管理-部门管理",action = "查询所有部门数据接口")
    @RequiresPermissions("sys:dept:list")
    public DataResult<List<SysDept>> getAll() {

        DataResult result = DataResult.success();

        result.setData(deptService.selectAll());

        return result;
    }

    @GetMapping("/dept/tree")
    @ApiOperation(value = "树形列表接口")
    @MyLog(title = "组织管理-部门管理",action = "部门树形结构列表接口")
    @RequiresPermissions(value = {"sys:user:update","sys:user:add","sys:dept:add","sys:dept:update"},logical = Logical.OR)
    public DataResult<List<DeptRespNodeVo>> getDeptTree(@RequestParam(required = false) String deptId) {

        DataResult<List<DeptRespNodeVo>> result = DataResult.success();

        result.setData(deptService.deptTreeList(deptId));

        return result;
    }

    @PostMapping("/dept")
    @ApiOperation(value = "新增部门接口")
    @MyLog(title = "组织管理-部门管理",action = "新增部门数据接口")
    @RequiresPermissions("sys:dept:add")
    public DataResult<SysDept> addDept(@RequestBody @Valid DeptAddReqVo vo) {

        DataResult<SysDept> result = DataResult.success();

        result.setData(deptService.addDept(vo));

        return result;
    }

    @PutMapping("/dept")
    @ApiOperation(value = "更新部门信息接口")
    @MyLog(title = "组织管理-部门管理",action = "更新部门数据接口")
    @RequiresPermissions("sys:dept:update")
    public DataResult updateDept (@RequestBody @Valid DeptUpdateReqVo vo){
        deptService.updateDept(vo);
        return DataResult.success();
    }

    @DeleteMapping("/dept/{id}")
    @ApiOperation(value = "删除部门接口")
    @MyLog(title = "组织管理-部门管理",action = "删除部门接口")
    @RequiresPermissions("sys:dept:delete")
    public DataResult deleteDept(@PathVariable("id") String id){
        deptService.deletedDept(id);
        return DataResult.success();
    }

}